<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="优惠券id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="id">-->
<!--              <a-input v-model="model.id" placeholder="请输入优惠券id"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="优惠券名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponName">
              <a-input v-model="model.couponName" placeholder="请输入优惠券名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠券介绍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponInfo">
              <a-input v-model="model.couponInfo" placeholder="请输入优惠券介绍"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发放主体 1平台发放，2分站发放，3商家发放" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponProvider">
              <a-input-number v-model="model.couponProvider" placeholder="请输入发放主体 1平台发放，2分站发放，3商家发放" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发放主体id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponProviderId">
              <a-input v-model="model.couponProviderId" placeholder="请输入发放主体id"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="可用类型 1全国，2分站，3商家" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useType">
              <a-input-number v-model="model.useType" placeholder="请输入可用类型 1全国，2分站，3商家" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="可用关联id 分站id或商家id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useTypeId">
              <a-input v-model="model.useTypeId" placeholder="请输入可用关联id 分站id或商家id"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠券总数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponAmount">
              <a-input v-model="model.couponAmount" placeholder="请输入优惠券总数量"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="可重复领取个数 默认1" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponCount">
              <a-input-number v-model="model.couponCount" placeholder="请输入可重复领取个数 默认1" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="特定可领取用户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="getUser">
              <a-input v-model="model.getUser" placeholder="请输入特定可领取用户"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="特定不可领取用户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="notGetUser">
              <a-input v-model="model.notGetUser" placeholder="请输入特定不可领取用户"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="领取条件 1全部 2新用户 3老用户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="getCondition">
              <a-input v-model="model.getCondition" placeholder="请输入领取条件 1全部 2新用户 3老用户"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="领取类型 1普通 2分享领券" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="getType">
              <a-input-number v-model="model.getType" placeholder="请输入领取类型 1普通 2分享领券" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠券类型 1满减券 2打折券" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponType">
              <a-input-number v-model="model.couponType" placeholder="请输入优惠券类型 1满减券 2打折券" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠券门槛" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponThreshold">
              <a-input v-model="model.couponThreshold" placeholder="请输入优惠券门槛"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠金额 如果是折扣存最大可抵金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponMoney">
              <a-input v-model="model.couponMoney" placeholder="请输入优惠金额 如果是折扣存最大可抵金额"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠折扣 折扣存0--1" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponDiscount">
              <a-input v-model="model.couponDiscount" placeholder="请输入优惠折扣 折扣存0--1"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠券生效时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date placeholder="请选择优惠券生效时间" v-model="model.startTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠券失效时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date placeholder="请选择优惠券失效时间" v-model="model.endTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优惠券状态 1正常 2删除" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <a-input-number v-model="model.status" placeholder="请输入优惠券状态 1正常 2删除" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'CouponForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           // id: [
           //    { required: true, message: '请输入优惠券id!'},
           // ],
        },
        url: {
          add: "/coupon/add",
          edit: "/coupon/edit",
          queryById: "/coupon/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>